//
//  AppDelegate.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/21.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func createItem() -> Void {
        let item1 = UIApplicationShortcutItem(type: "1", localizedTitle: "第1个标签")
        let item2 = UIApplicationShortcutItem(type: "2", localizedTitle: "第2个标签", localizedSubtitle: "子标题", icon: UIApplicationShortcutIcon(type: .add), userInfo: nil)
        let item3 = UIApplicationShortcutItem(type: "3", localizedTitle: "第3个标签")
        let item4 = UIApplicationShortcutItem(type: "4", localizedTitle: "第4个标签")
        
        UIApplication.shared.shortcutItems = [item1,item2,item3,item4]
    }
    
    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        print("=======\(shortcutItem.type)")
        completionHandler(true)
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        createItem()
        // 音频播放
        let session = AVAudioSession.sharedInstance()
        try? session.setActive(true, options: .init())
        try? session.setCategory(.playback, options: .init())
        UIApplication.shared.beginReceivingRemoteControlEvents()
        return true
    }
    
    override func remoteControlReceived(with event: UIEvent?) {
        if event?.type == UIEvent.EventType.remoteControl {
            switch event?.subtype {
            case .remoteControlPlay:
                print("remoteControlPlay")
            case .remoteControlTogglePlayPause:
                print("remoteControlTogglePlayPause")
            case .remoteControlNextTrack:
                print("remoteControlNextTrack")
            case .remoteControlPreviousTrack:
                print("remoteControlPreviousTrack")
            default:
                print("other")
            }
        }
    }
    
    // MARK: UISceneSession Lifecycle
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    
    
}

